package com.zxj.geekstudy.controller;

import com.google.common.collect.Lists;
import com.zxj.geekstudy.model.UserOrderLog;
import com.zxj.geekstudy.model.UserOrderSnap;
import com.zxj.geekstudy.service.UserOrderLogService;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping(path = "order")
public class OrderController {

    @Resource
    private UserOrderLogService userOrderLogService;

    @RequestMapping(path = "create")
    public String create() throws SQLException {
        // 模拟请求参数
        Date now = new Date();
        List<UserOrderLog> userOrderLogs = Lists.newArrayList();
        for (int i = 0; i < 10; i++) {
            UserOrderLog userOrderLog = new UserOrderLog();
            userOrderLog.setUserId(new Random().nextInt(10));
            userOrderLog.setOrderDetail("{}");
            userOrderLog.setAddressId(1);
            userOrderLog.setCreateTime(now);
            userOrderLog.setUpdateTime(now);
            userOrderLogs.add(userOrderLog);
        }
//        TransactionTypeHolder.set(TransactionType.XA);
//        userOrderLogService.addUserOrderLogs(userOrderLogs);
        userOrderLogService.addUserOrderLogsXATransaction(userOrderLogs);
        return "success";
    }

    @RequestMapping(path = "list", method = {RequestMethod.GET})
    public List<UserOrderLog> list(UserOrderLog userOrderLog) {
        return userOrderLogService.list(userOrderLog);
    }
}
